//
//  ViewController.m
//  XMMessageKit
//
//  Created by lishi on 2019/3/5.
//  Copyright © 2019 lishi. All rights reserved.
//

#import "ViewController.h"

// View
#import "XMBaseMessageTableCell.h"
#import "XMMessageTextTableCell.h"
#import "XMMessageImageTableCell.h"

//
#import "Masonry.h"
#import "XMMessageFactory.h"

@interface ViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *tableArray;

@end

static NSString *const messageTextcellID = @"messageTextcellID";
static NSString *const messageImagecellID = @"messageImagecellID";

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if(@available(iOS 11.0, *)) { //方法一适配
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.edges.equalTo(self.view);
        }
    }];
    
    XMMessageModel *textModel = [[XMMessageModel alloc] init];
    textModel.messageType = @"XMMessageTextTableCell";
    textModel.messageName = @"小明明";
    XMMessageModel *imageModel = [[XMMessageModel alloc] init];
    imageModel.messageType = @"XMMessageImageTableCell";
    imageModel.messageName = @"小米米";
    imageModel.messageUrl = @"http://img5.duitang.com/uploads/item/201206/15/20120615031447_R5EcS.jpeg";

    
    // 模拟数据
    self.tableArray = @[
                        imageModel,
                        textModel,
                        imageModel,
                        imageModel,
                        imageModel,
                        textModel,
                        imageModel,
                        textModel,
                        textModel
                        ];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     return [XMMessageFactory creatCellWithModel:self.tableArray[indexPath.row]];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
